/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.editor;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import info.xmlprint.core.format.FormatGroup;
import info.xmlprint.core.mapping.MappingGroup;
import info.xmlprint.core.xml.PElementGroup;
import info.xmlprint.run.Configuration;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

public class OpenEditor {
	/*
	private Logger logger = Logger.getLogger(this.getClass().getName());

	private final String FILE_SEPARATOR = System.getProperties().getProperty(
			"file.separator");

	private String projectName;

	public OpenEditor(String fileName) {
		if (fileName.matches(".*(.xmlp)")) {
			if (readXMLP(fileName)) {
				StileditorManager.setImportFilePath(fileName);
			} else {
				StileditorManager.setImportFilePath(null);
			}

		} else if (fileName.matches(".*(.xml)")) {
			readXML(fileName);
			StileditorManager.setImportFilePath(null);
		}
	}

	private boolean readXMLP(String xmlpName) {
		File xmlpFile = new File(xmlpName);
		projectName = xmlpFile.getName();
		try {
			FileInputStream fileInputStream = new FileInputStream(xmlpFile);

			ZipInputStream zipinputstream = new ZipInputStream(fileInputStream);

			ZipEntry ze;
			String entryName;
			String entryDir;
			File entryFile;
			String xmlToOpen;

			MappingGroup mg = new MappingGroup();
			FormatGroup fg = new FormatGroup();
			mg.setFormatGroup(fg);

			while ((ze = zipinputstream.getNextEntry()) != null) {
				entryName = ze.getName();
				entryName = entryName.replace("\\", FILE_SEPARATOR);
				entryFile = new File(entryName);
				entryDir = entryFile.getParent();

				if (entryDir == null)
					continue;

				byte[] buf = new byte[1024];
				int n;

				if (entryDir.equalsIgnoreCase("Content")) {
					xmlToOpen = Configuration.getInstance().getXmlprintTmpDir()
							+ entryFile.getName();
					FileOutputStream fos = new FileOutputStream(xmlToOpen);
					BufferedOutputStream bos = new BufferedOutputStream(fos,
							2048);

					while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
						bos.write(buf, 0, n);
					}

					bos.close();
					fos.close();
					readXML(xmlToOpen);
					PElementGroup pg = StileditorManager.getPElementGroup();
					mg.setMainNamespacePrefix(pg.getMainNamespacePrefix());
					mg.setMainNamespaceUri(pg.getMainNamespaceUri());
				} else if (entryDir.equalsIgnoreCase("Mapping")) {
					ByteArrayOutputStream bout = new ByteArrayOutputStream();

					while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
						bout.write(buf, 0, n);
					}

					InputStream is = new ByteArrayInputStream(
							bout.toByteArray());

					try {
						SAXReader saxReader = new SAXReader();
						saxReader.setEncoding("utf-8");
						Document doc = saxReader.read(is);
						mg.readXmlDoc(doc, false, null);
						StileditorManager.setFormatGroup(mg.getFormatGroup());
						StileditorManager.setMappingGroup(mg);

					} catch (DocumentException e) {
						logger.error("", e);
						e.printStackTrace();
					}
					is.close();
				}

				zipinputstream.closeEntry();
			}
			zipinputstream.close();
			return true;

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	private void readXML(String fileName) {
		File fileFromTempDir = new File(fileName);

		IWorkbenchPage page = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();

		String editorID = "info.xmlprint.ui.xmleditor";
		IStorageEditorInput input = new EditorInput(new FileStorage(
				fileFromTempDir, projectName));
		try {
			page.openEditor(input, editorID);
			// IMenuService vMenuService = (IMenuService)
			// PlatformUI.getWorkbench().getService(IMenuService.class);
		} catch (PartInitException e) {
			logger.error("", e);
			e.printStackTrace();
		}

	}
*/
}
